package com.springboot.quanxian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.quanxian.common.ResultEnum;
import com.springboot.quanxian.entity.Notice;
import com.springboot.quanxian.exception.MyException;
import com.springboot.quanxian.mapper.NoticeMapper;
import com.springboot.quanxian.qpage.QPage;
import com.springboot.quanxian.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.quanxian.util.ResultView;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {



    @Override
    public ResultView getPageList(QPage qPage, Notice obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<Notice>();
        if(!StringUtils.isEmpty(obj.getTitle())){
            queryWrapper.like("title",obj.getTitle());
        }
        queryWrapper.orderByDesc("add_time");
        IPage mapList =this.page(iPage,queryWrapper);
        return ResultView.ok(mapList);
    }

    @Override
    public List<Notice> getSelectList() {
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();
        return this.list(queryWrapper);
    }

    @Transactional
    @Override
    public boolean add(Notice obj) {
        Notice dto=new Notice();
        BeanUtils.copyProperties(obj,dto);
        dto.setAddTime(new Date());
        return this.save(dto);
    }

    @Transactional
    @Override
    public boolean del(String id) {
        Notice dto=this.getById(id);
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"找不到此数据，删除异常");
        }
        return this.removeById(dto);
    }


    @Transactional
    @Override
    public boolean up(Notice obj) {
        Notice dto=this.getById(obj.getNoticeId());
        if(dto==null){
            throw new MyException(ResultEnum.CODE_2,"找不到此数据，修改异常");
        }
        BeanUtils.copyProperties(obj,dto,new String[]{"noticeId"});
        dto.setUpdateTime(new Date());
        return this.updateById(dto);
    }



}
